package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC670 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int maximumSwap(int num) {
            StringBuilder numStr = new StringBuilder(String.valueOf(num));
            int[] biggerIdx = new int[numStr.length()];
            for (int i = numStr.length() - 1; i >= 0; i--) {
                if (i == numStr.length() - 1) biggerIdx[i] = numStr.length() - 1;
                else
                    biggerIdx[i] = (numStr.charAt(biggerIdx[i + 1]) - '0') >= (numStr.charAt(i) - '0') ? biggerIdx[i + 1] : i;
            }

            for (int i = 0; i < numStr.length(); i++) {
                if (numStr.charAt(i) - '0' < numStr.charAt(biggerIdx[i]) - '0') {
                    char temp = numStr.charAt(i);
                    numStr.setCharAt(i, numStr.charAt(biggerIdx[i]));
                    numStr.setCharAt(biggerIdx[i], temp);
                    break;
                }
            }

            return Integer.parseInt(numStr.toString());
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.maximumSwap(9973));
    }

    public static void main(String[] args) throws Exception {
        LC670 an = new LC670();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
